<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //创建一个构造函数 Calculator，以创建“可扩展”的 calculator 对象。
        function Calculator() {
            this.method = {
                '+': (a, b) => a + b,
                '-': (a, b) => a - b,
                '*': (a, b) => a * b,
                '/': (a, b) => a / b,
                '^': (a, b) => Math.pow(a, b)
            }
            this.calculator = function (str) {
                //用空格来分割
                var arr = str.split(" ");
                console.log(arr);
                if (this.method[arr[1]]) {
                    return this.method[arr[1]](Number(arr[0]), Number(arr[2]))
                }
            }
            this.addMethod = function (name, func) {
                this.method[name] = func;
                console.log(this.method);
            }
        }
        var cale = new Calculator();
        console.log(cale.calculator("12 + 3"));
    </script>
</body>

</html>